﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Linq;

namespace PrinterBudi.Persistence
{
    public class PBUserModel
    {
        public static void Delete(PBUser existingUser)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(existingUser);
                    transaction.Commit();
                }
            }
        }

        public static void Update(PBUser newUserName)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(newUserName);
                    transaction.Commit();
                    Console.WriteLine("Updated User: " + newUserName.PhoneNumber);
                }
            }
        }

        public static PBUser Read(string userPhoneNumber)
        {
            try
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    var userQuery = (from usertemp in session.Query<PBUser>()
                                     where usertemp.PhoneNumber == userPhoneNumber
                                     select usertemp).Single();

                    return userQuery;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

        public static PBUser Read(int userId)
        {
            try
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    var userQuery = (from usertemp in session.Query<PBUser>()
                                     where usertemp.Id == userId
                                     select usertemp).Single();

                    return userQuery;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

        public static List<PBUser> ReadAllUser()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var userQuery = (from usertemp in session.Query<PBUser>()
                                 select usertemp).ToList();

                Console.WriteLine("Read User: " + userQuery.Count);
                return userQuery;
            }
        }

        public static bool Create(string phoneNumber, string password, string name, string address, int verSuccess)
        {
            try
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        if (Read(phoneNumber) != null) return false;

                        var usertemp = new PBUser
                        {
                            PhoneNumber = phoneNumber,
                            Password = password,
                            Address = address,
                            Name = name,
                            VerSuccess = verSuccess,
                        };
                        session.Save(usertemp);

                        transaction.Commit();
                        Console.WriteLine("Created Transaction: " + usertemp.PhoneNumber);

                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

    }
}
